﻿using MediatR;
using Traceability.APP.Application.Command.Breeds;
using Traceability.Damoin;
using Traceability.ErrorCode;
using Traceability.Infaseture;

namespace Traceability.APP.Application.CommandHadnLer.Breeds
{
    public class GetEnterpriseCommandHandLer : IRequestHandler<GetEnterpriseCommand, APIResult<List<Enterprise>>>
    {

        private readonly IBaseRepostiy<Enterprise> _enterprise;

        public GetEnterpriseCommandHandLer(IBaseRepostiy<Enterprise> enterprise)
        {
            _enterprise = enterprise;
        }

        public async Task<APIResult<List<Enterprise>>> Handle(GetEnterpriseCommand request, CancellationToken cancellationToken)
        {
            // 获取所有养殖信息
            var list = _enterprise.GetAll().Where(x=>!x.IsDeleted);

            // 根据批次筛选
            list = list.Where(x => x.Batch == request.Batch);

            // 按照 Id 降序排序
            list = list.OrderByDescending(x => x.Id);
            return new APIResult<List<Enterprise>>
            {
                Code = ResultCode.Ok,
                Data = list.ToList(),
                Message = "获取成功"
            };
        }
    }
}
